﻿using System.Reflection;
using System.Windows;
using System.Windows.Controls;


using WiseLinkWPF.Windows.Controls;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Data;

namespace CCF20
{
    /// <summary> 
    /// Interaction logic for ComboColumnFilteringControl.xaml 
    /// </summary> 
    public partial class ComboColumnFilteringControl : UserControl, IFilteringControl
    {
        private GridViewComboBoxColumn column;
        private CompositeFilterDescriptor compositeFilterDescriptor;

        public ComboColumnFilteringControl()
        {
            InitializeComponent();
        }

        #region IFilteringControl Members

        /// <summary> 
        /// Gets or sets a value indicating whether the filtering is active. 
        /// </summary> 
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        /// <summary> 
        /// Identifies the <see cref="IsActive"/> dependency property. 
        /// </summary> 
        public static readonly DependencyProperty IsActiveProperty =
        DependencyProperty.Register(
        "IsActive",
        typeof(bool),
        typeof(ComboColumnFilteringControl),
        new System.Windows.PropertyMetadata(false));

        public void Prepare(WiseLinkWPF.Windows.Controls.GridViewBoundColumnBase column)
        {
            this.column = column as GridViewComboBoxColumn;

            this.lblSelect.Content = string.Format("请选择{0}:", this.column.Header);
            
            if (this.compositeFilterDescriptor == null)
            {
                this.compositeFilterDescriptor = new CompositeFilterDescriptor() { LogicalOperator = FilterCompositionLogicalOperator.Or };
                this.column.DataControl.FilterDescriptors.Add(compositeFilterDescriptor);
            }
        }

        #endregion IFilteringControl Members

        private void listBoxDistinctValues_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            compositeFilterDescriptor.FilterDescriptors.Clear();

            foreach (var item in this.listBoxDistinctValues.SelectedItems)
            {
                PropertyInfo propertyInfo = item.GetType().GetProperty((this.column).SelectedValueMemberPath);
                object itemValue = propertyInfo.GetValue(item, null);
                compositeFilterDescriptor.FilterDescriptors.Add(new FilterDescriptor(this.column.DataMemberBinding.Path.Path, FilterOperator.IsEqualTo, itemValue));
            }

            if (this.compositeFilterDescriptor.FilterDescriptors.Count > 0)
            {
                if (!this.column.DataControl.FilterDescriptors.Contains(this.compositeFilterDescriptor))
                {
                    this.column.DataControl.FilterDescriptors.Add(this.compositeFilterDescriptor);
                }
                this.SetValue(IsActiveProperty, true);
            }
            else
            {
                if (this.column.DataControl.FilterDescriptors.Contains(this.compositeFilterDescriptor))
                {
                    this.column.DataControl.FilterDescriptors.Remove(this.compositeFilterDescriptor);
                }
                this.SetValue(IsActiveProperty, false);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.listBoxDistinctValues.ItemsSource = this.column.ItemsSource;
            
            //this.listBoxDistinctValues.ItemsSource = this.column.ItemsSource;
            //this.listBoxDistinctValues.DisplayMemberPath = this.column.DisplayMemberPath;

        }
    }
}


